//
//  MineHeaderView.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/13.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "MineHeaderView.h"
#import "UIImage+Color.h"

@interface MineHeaderView ()

@property (nonatomic, strong) UIImageView *headerBgView;
@property (nonatomic, strong) UIView *avatarBgView;
@property (nonatomic, strong) UIImageView *avatar;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UILabel *telLab;
@property (nonatomic, strong) UIImageView *arrow;
@property (nonatomic, strong) UIView *cartBgView;
@property (nonatomic, strong) UIImageView *cartIcon;
@property (nonatomic, strong) UILabel *cartNameLab;
@property (nonatomic, strong) UILabel *cartNumLab;
@property (nonatomic, strong) UILabel *cartBillLab;
@property (nonatomic, strong) UIImageView *cartArrow;

@property (nonatomic, strong) UIButton *mineInfoButton;

@end

@implementation MineHeaderView

- (void)setUserInfoEntity:(UserInfoEntity *)userInfoEntity
{
    _userInfoEntity = userInfoEntity;
    self.nameLab.text = userInfoEntity.orgName;
    self.telLab.text = userInfoEntity.telephone;
    if (userInfoEntity.memberKind && userInfoEntity.memberKind.intValue == 2) {
        self.cartNameLab.text = userInfoEntity.typeName;
        self.cartBgView.userInteractionEnabled = YES;
        self.cartBgView.hidden = NO;
    }
    else {
        self.cartNameLab.text = @"体验会员";
        self.cartBgView.userInteractionEnabled = NO;
    }
    self.cartNumLab.text = userInfoEntity.orgCode;
    if (userInfoEntity.recentRepayTime) {
        self.cartBillLab.text = [NSString stringWithFormat:@"最近还款:%@",String(userInfoEntity.recentRepayTime)];
    }
    if (userInfoEntity.filePath.length > 0) {
        self.avatarBgView.userInteractionEnabled = NO;
    }
    else {
        self.avatarBgView.userInteractionEnabled = YES;
    }
    [self.avatar sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVERADDRS,userInfoEntity.filePath]] placeholderImage:[UIImage imageNamed:@"person_headpic"]];
}

- (void)mineInfoAction:(id)sender
{
    if (self.mineInfoHandler) {
        self.mineInfoHandler();
    }
}

- (void)cartAciton:(UITapGestureRecognizer *)tap
{
    if (self.mineCartHandler) {
        self.mineCartHandler();
    }
}

- (void)setName:(NSString *)name
{
    _name = name;
    self.nameLab.text = name;
}

- (void)setTel:(NSString *)tel
{
    _tel = tel;
    self.telLab.text = tel;
}

- (void)setupSubViews
{
    self.headerBgView = [[UIImageView alloc] init];
    self.headerBgView.image = [UIImage gradientImageWithColorArray:@[(id)UIColorFromRGB(0x1479f7).CGColor,(id)UIColorFromRGB(0x6eadf3).CGColor] beginPoint:CGPointMake(0, 0) endPoint:CGPointMake(kScreenW, 0) rect:CGRectMake(0, 0, kScreenW, 2000)];
    [self addSubview:self.headerBgView];
    
    self.mineInfoButton = [[UIButton alloc] init];
    self.mineInfoButton.backgroundColor = [UIColor clearColor];
    [self addSubview:self.mineInfoButton];
    [self.mineInfoButton addTarget:self action:@selector(mineInfoAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.avatarBgView = [[UIView alloc] init];
    self.avatarBgView.backgroundColor = [UIColor whiteColor];
//    self.avatarBgView.alpha = 0.4;
    self.avatarBgView.layer.cornerRadius = 56/2;
    self.avatarBgView.layer.masksToBounds = YES;
    self.avatarBgView.userInteractionEnabled = NO;
    [self addSubview:self.avatarBgView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAvatar:)];
    [self.avatarBgView addGestureRecognizer:tap];
    
    self.avatar = [[UIImageView alloc] init];
    self.avatar.layer.cornerRadius = 50/2;
    self.avatar.layer.masksToBounds = YES;
    self.avatar.userInteractionEnabled = YES;
    self.avatar.backgroundColor = [UIColor whiteColor];
    [self.avatarBgView addSubview:self.avatar];
    
    self.nameLab = [[UILabel alloc] init];
    self.nameLab.textColor = [UIColor whiteColor];
    self.nameLab.font = [UIFont systemFontOfSize:16];
    [self addSubview:self.nameLab];
    
    self.telLab = [[UILabel alloc] init];
    self.telLab.textColor = [UIColor whiteColor];
    self.telLab.font = [UIFont systemFontOfSize:16];
    [self addSubview:self.telLab];
    
    self.arrow = [[UIImageView alloc] init];
    self.arrow.image = [UIImage imageNamed:@"arrow_rightwhite"];
    [self addSubview:self.arrow];
    
    self.cartBgView = [[UIView alloc] init];
    self.cartBgView.backgroundColor = UIColorFromRGB(0x2D78D4);
    self.cartBgView.userInteractionEnabled = YES;
    [self.cartBgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cartAciton:)]];
    [self addSubview:self.cartBgView];
    if (UserUtils.shared.showVip) {
        self.cartBgView.hidden = NO;
    }
    else {
        self.cartBgView.hidden = YES;
    }
    
    self.cartIcon = [[UIImageView alloc] init];
    self.cartIcon.image = [UIImage imageNamed:@"gold"];
    [self.cartBgView addSubview:self.cartIcon];
    
    self.cartNameLab = [[UILabel alloc] init];
    self.cartNameLab.textColor = [UIColor whiteColor];
    self.cartNameLab.font = [UIFont systemFontOfSize:16];
    [self.cartBgView addSubview:self.cartNameLab];
    
    self.cartNumLab = [[UILabel alloc] init];
    self.cartNumLab.textColor = [UIColor whiteColor];
    self.cartNumLab.font = [UIFont boldSystemFontOfSize:15];
    [self.cartBgView addSubview:self.cartNumLab];
    
    self.cartBillLab = [[UILabel alloc] init];
    self.cartBillLab.textColor = [UIColor whiteColor];
    self.cartBillLab.font = [UIFont systemFontOfSize:12];
    [self.cartBgView addSubview:self.cartBillLab];
    
    self.cartArrow = [[UIImageView alloc] init];
    self.cartArrow.image = [UIImage imageNamed:@"arrow_right"];
    [self.cartBgView addSubview:self.cartArrow];
    
    
    
//    self.nameLab.text = @"呵呵呵呵";
//    self.telLab.text = @"1231312313";
//    self.cartNameLab.text = @"铜牌会员";
//    self.cartNumLab.text = @"VIP000001";
//    self.cartBillLab.text = @"最近还款:2020.10.10";
}

- (void)setupConstraints
{
    [self.headerBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.cartBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self);
        make.height.equalTo(@(kWidth(55)));
    }];
    
    [self.cartIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.cartBgView);
        make.left.equalTo(self.cartBgView).offset(kWidth(20));
    }];
    
    [self.cartNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.cartIcon);
        make.left.equalTo(self.cartIcon.mas_right).offset(kWidth(10));
    }];
    
    [self.cartNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.cartBgView.mas_centerY);
        make.right.equalTo(self.cartBgView).offset(-kWidth(35));
    }];
    
    [self.cartBillLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.cartBgView).offset(-kWidth(35));
        make.top.equalTo(self.cartBgView.mas_centerY).offset(kWidth(8));
    }];
    
    [self.cartArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.cartIcon);
        make.right.equalTo(self.cartBgView).offset(-kWidth(15));
    }];
    
    [self.avatarBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(kWidth(20));
        make.bottom.equalTo(self.cartBgView.mas_top).offset(-kWidth(16));
        make.width.height.equalTo(@56);
    }];
    
    [self.avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.avatarBgView);
        make.width.height.equalTo(@50);
    }];
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.avatarBgView.mas_right).offset(kWidth(20));
        make.top.equalTo(self.avatarBgView).offset(kWidth(5));
    }];
    
    [self.telLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLab);
        make.bottom.equalTo(self.avatarBgView).offset(-kWidth(5));
    }];
    
    [self.arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.avatarBgView);
        make.right.equalTo(self).offset(-kWidth(20));
    }];
    
    [self.mineInfoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.avatarBgView);
        make.left.right.equalTo(self);
    }];
}

- (void)clickAvatar:(UITapGestureRecognizer *)tap
{
    if (self.mineAvatarHandler) {
        self.mineAvatarHandler();
    }
}

@end
